package com.cqsd.ioc.handler;

import com.cqsd.ioc.exception.HandleAddException;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * @author caseycheng
 * TODO 不用
 * @date 2023/3/11-17:17
 **/
@Deprecated
public class InterfaceHandler {
    private final Map<Class<?>, Consumer<Object>> interfaceHandlerMap = new HashMap<>();

    //传入一个对象，获取该对象的所有接口并查找方法进行执行
    //为什么没有接口类型的基类啊！！！！
    public void handler(Object type) {
        final var interfaces = type.getClass().getInterfaces();
        for (Class<?> f : interfaces) {
            final Consumer<Object> h = interfaceHandlerMap.get(f);
            if (h != null) {
                h.accept(type);
            }
        }
    }
    //添加一个接口处理器
    public <I> void addHandler(Class<I> type, Consumer<Object> action) {
        //泛型参数 好痛
        Objects.requireNonNull(action);
        if (interfaceHandlerMap.containsKey(type)){
            throw new HandleAddException("重复添加"+type.getSimpleName());
        }
        interfaceHandlerMap.put(type, action);
    }

}
